package com.ycdx.impms.utils;

import java.sql.Connection;
import java.sql.SQLException;
import java.beans.PropertyVetoException;
import com.mchange.v2.c3p0.ComboPooledDataSource;

public class DBPool {
	private static DBPool impmsDbPool;
	private ComboPooledDataSource impmsDataSource;
	private final static String USER = "root";
	private final static String PASSWORD = "t2000vip";

	static {
		impmsDbPool = new DBPool();
	}

	public DBPool() {
		try {
			impmsDataSource = new ComboPooledDataSource();
			impmsDataSource.setUser(USER);
			impmsDataSource.setPassword(PASSWORD);
			impmsDataSource
					.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/impms?autoReconnect=true&useUnicode=true");
			impmsDataSource.setDriverClass("com.mysql.jdbc.Driver");
			impmsDataSource.setInitialPoolSize(3);
			impmsDataSource.setAcquireIncrement(3);
			impmsDataSource.setAcquireRetryAttempts(30);
			impmsDataSource.setAcquireRetryDelay(1000);
			impmsDataSource.setAutoCommitOnClose(false);
			impmsDataSource.setAutomaticTestTable("Test");
			impmsDataSource.setCheckoutTimeout(0);
			impmsDataSource.setForceIgnoreUnresolvedTransactions(false);
			impmsDataSource.setNumHelperThreads(3);
			impmsDataSource.setIdleConnectionTestPeriod(60);
			impmsDataSource.setMinPoolSize(1);
			impmsDataSource.setMaxPoolSize(40);
			impmsDataSource.setMaxStatements(100);
			impmsDataSource.setMaxIdleTime(60);
		} catch (PropertyVetoException e) {
			throw new RuntimeException(e);
		}
	}

	public final static DBPool getInstance() {
		return impmsDbPool;
	}

	public final Connection getConnection() {
		//System.out.println("conn");
		try {
			return impmsDataSource.getConnection();
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException("无法从数据源获取连接 ", e);
		}
	}
	/*
	 public static void main(String[] args) throws SQLException {
	 Connection con = null;
	 try {
	 con = DBPool.getInstance().getConnection();
	 } catch (Exception e) {
	 } finally {
	 if (con != null)
	 con.close();
	 }
	 }
	 */
}